package com.yh.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.encrypt.strategy.spi.Encryptor;

import java.util.Properties;

@Data
@Slf4j
public class AesDesensitization implements Encryptor {

    private Properties properties = new Properties();

    private static final String AES_KEY = "12345678abcdefgh";

    @Override
    public void init() {

    }

    /**
     * 加密
     * @param o
     * @return
     */
    @Override
    public String encrypt(Object o) {
        log.info("原始数据:"+o);
        String encryptStr = null;
        try {
            encryptStr = AESUtil.encryption(o.toString(),AES_KEY);
        } catch (Exception e) {
            log.error("数据脱敏加密失败",o,e);
            encryptStr = o.toString();
        }
        log.info("加密数据:"+encryptStr);
        return encryptStr;
    }

    /**
     * 解密
     * @param decryptStr
     * @return
     */
    @Override
    public Object decrypt(String decryptStr) {
        log.info("加密数据:"+decryptStr);
        try {
            decryptStr = AESUtil.decrypt(decryptStr,AES_KEY);
        } catch (Exception e) {
            log.error("数据脱敏解密失败",decryptStr,e);
        }
        log.info("原始数据:"+decryptStr);
        return decryptStr;
    }

    /**
     * 类型
     * @return
     */
    @Override
    public String getType() {
        return "AES_CUSTOM";
    }


}
